/*
 * Copyright (c) 2000-2019 TeamDev Ltd. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */
package samples;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.Callback;
import com.teamdev.jxbrowser.chromium.XPathResultType;
import com.teamdev.jxbrowser.chromium.dom.DOMAttributeNode;
import com.teamdev.jxbrowser.chromium.dom.DOMDocument;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 * This sample demonstrates how to work with an attribute node obtained via JxBrowser XPath API.
 */
public class DOMAttributeSample {

    public static void main(String[] args) {
        final Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(view, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
            @Override
            public void invoke(Browser value) {
                browser.loadHTML("<a id='link' href='https://teamdev.com/'>TeamDev</a>");
            }
        });

        DOMDocument document = browser.getDocument();
        DOMAttributeNode hrefAttr = (DOMAttributeNode) document
                .evaluate("//a/@href", XPathResultType.ANY_UNORDERED_NODE_TYPE)
                .getSingleNode();
        System.out.println("Attribute value: " + hrefAttr.getNodeValue()); // https://teamdev.com
        System.out.println("Attribute owner: " + hrefAttr.getOwnerElement().getNodeName()); // A
    }
}
